package com.cskaoyan.wordmemorize.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.converter.TopicConverter;
import com.cskaoyan.wordmemorize.dao.entity.ReviewTopicDO;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicOptionMapper;
import com.cskaoyan.wordmemorize.dao.mapper.TopicOptionsMapper;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.TopicCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.TopicService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.cskaoyan.wordmemorize.auth.StpKit.ADMIN;

@RestController
public class TopicController {

    @Autowired
    TopicService topicService;
    @GetMapping("/admin/topic")
    public Result<ReviewTopicDTO> list(@RequestParam(value = "topicId") String topicId) {
        System.out.println(topicId);
        //根据topicId查询topic
        ReviewTopicDTO topic = topicService.getTopic(Long.valueOf(topicId));
        return Result.ok(topic);
    }

    @PostMapping("/admin/topic")
    @SaAdminCheckPermission({SystemPermissions.Topic.ADD})
    public Result addTopic(@RequestBody TopicCommand command) {
        System.out.println(command);
        topicService.addTopic(command);
        return Result.ok();
    }

    @PutMapping("/admin/topic")
    @SaAdminCheckPermission({SystemPermissions.Topic.UPDATE})
    public Result updateTopic(@RequestBody TopicCommand command) {
        topicService.updateTopic(command);
        return Result.ok();
    }

    @GetMapping("/admin/topic/word")
    public Result<VocDTO> checkWord(@RequestParam(value = "word") String word) {
        VocDTO vocDTO = topicService.checkWord(word);
        System.out.println(vocDTO);
        return Result.ok(vocDTO);
    }

    @GetMapping("/admin/topic/singleChoice/list")
    @SaAdminCheckPermission({SystemPermissions.Topic.SEARCH})
    public  Result<PageDTO<ReviewTopicDTO>> getSingleChoicePage( VocPageRequest vocPageRequest) {
        System.out.println(vocPageRequest);
        PageDTO<ReviewTopicDTO> singleChoicePage = topicService.getSingleChoicePage(vocPageRequest);
        System.out.println(singleChoicePage);
        return Result.ok(singleChoicePage);
    }

    @GetMapping("/admin/topic/fillWord/list")
    @SaAdminCheckPermission({SystemPermissions.Topic.SEARCH})
    public  Result<PageDTO<ReviewTopicDTO>> getFillWordPage( VocPageRequest vocPageRequest) {
        System.out.println(vocPageRequest);
        PageDTO<ReviewTopicDTO> singleChoicePage = topicService.getFillWordPage(vocPageRequest);
        return Result.ok(singleChoicePage);
    }

    @DeleteMapping("/admin/topic/option/{id}")
    public Result<DeleteTopicOptionOperateDTO> deleteTopicOption(@PathVariable("id") Long id) {
        DeleteTopicOptionOperateDTO deleteTopicOption = topicService.deleteTopicOption(id);
        return Result.ok(deleteTopicOption);
    }

    @DeleteMapping("/admin/subtopic/{id}")
    public Result<DeleteSubTopicOperateDTO> deleteSubTopic(@PathVariable("id") Long id) {
        DeleteSubTopicOperateDTO deleteSubTopicOperateDTO = topicService.deleteSubTopic(id);
        return Result.ok(deleteSubTopicOperateDTO);
    }

    @DeleteMapping("/admin/topic/{topicId}")
    @SaAdminCheckPermission({SystemPermissions.Topic.DELETE})
    public Result<DeleteTopicOperateDTO> deleteTopic(@PathVariable("topicId") Long topicId, @RequestParam("topicType") Integer topicType) {
        DeleteTopicOperateDTO deleteTopicOperateDTO = topicService.deleteTopic(topicId, topicType);
        return Result.ok(deleteTopicOperateDTO);
    }


    @PostMapping("/admin/topic/excel/validation/{type}")
    public Result topicExcelValidate(@PathVariable("type") String type, @RequestBody MultipartFile file) throws IOException {
        System.out.println("MultipartFile.class = " + MultipartFile.class);
        topicService.topicExcelValidate(type, file);
        return Result.ok();
    }

    @PostMapping("/admin/topic/singleChoice")
    public Result<ExcelAddOperateDTO> addSingleExcel(@RequestBody MultipartFile file) throws IOException {
        long loginId = Long.parseLong((String) ADMIN.getLoginId());
        ExcelAddOperateDTO excelAddOperateDTO = topicService.addSingleChoiceExcel(file, loginId);
        return Result.ok(excelAddOperateDTO);
    }

    @PostMapping("/admin/topic/fillWord")
    public Result<ExcelAddOperateDTO> addFillWordExcel(@RequestBody MultipartFile file) throws IOException {
        long loginId = Long.parseLong((String) ADMIN.getLoginId());
        ExcelAddOperateDTO excelAddOperateDTO = topicService.addFillWordExcel(file, loginId);
        return Result.ok(excelAddOperateDTO);
    }





}
